#include "mvc/model/geometry.h"

Geometry::Geometry() : _components(0)
{

}

Geometry::~Geometry()
{
    geometry.clear();
    for(int i = 0; i < _components.size(); i++)
        if(_components[i] != 0)
            delete _components[i];
}

/**
  * calculates the points of the all components
  */
void Geometry::calculate_points()
{
    for(int i = 0; i < _components.size(); i++)
        _components[i]->calculate_points();
}

/**
  * return the row size. All the componentes have the same row size
  */
int Geometry::rows() const
{
    return _components.at(0)->rows();
}

/**
  * calculate the total points of the geometry.
  * repeat - if its false, the union points of two components only count once.
  */
int Geometry::total_points(bool repeat) const
{
    int _total_points = 0;

    for(int i = 0; i < _components.size(); i++)
    {
        int c_totalp = _components[i]->total_points(repeat);

        Station *s_aux = dynamic_cast<Station *> (_components[i]);

        if(i == 0 || !s_aux || repeat)
            _total_points += c_totalp;
        else
            _total_points += (c_totalp - s_aux->rows());
    }

    return _total_points;
}

/**
  * returns the total components
  */
int Geometry::total_components() const
{
    return _components.size();
}

/**
  * return the points of the geometry
  * repeat - if its false, the union points of two components only returns once.
  */
const QVector<Point3D *> &Geometry::get_points(bool repeat) const
{
    geometry.clear();
    for(int i = 0; i < _components.size(); i++)
    {
        QVector<Point3D *> values = _components[i]->get_points(repeat);

        int j = (i == 0 || repeat)? 0 : _components[i]->rows();

        for(; j < values.size(); j++)
            geometry.push_back(values.at(j));
    }

    return geometry;
}

/**
  * adds a new component to the geometry
  */
void Geometry::add_component(AGeometry *component)
{
    _components.push_back(component);
}

/**
  *
  */
void Geometry::remove_component(AGeometry *component)
{
}

/**
  * get a component of the geometry
  * index - the index of the componets to return.
  */
const AGeometry *Geometry::get_component(int index) const throw(Non_Existent_Geometry)
{
    if(_components.size() < index || index < 0)
        throw Non_Existent_Geometry();
    else
        return _components.at(index);
}
